package com.tv.nextin;

import java.util.ArrayList;

import org.w3c.dom.Text;

import com.tv.nextin.UIControls.MyGridView;
import com.tv.nextin.Utils.Common;
import com.tv.nextin.adapter.GridItemAdapter;
import com.tv.nextin.bean.Bussiness;
import com.tv.nextin.bean.TimeSp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class AllAppointmentActivity extends Activity implements OnTouchListener {

	TextView title, txtName;
	ImageButton btnBack;

	MyGridView grToday;
	MyGridView grTm, gr1, gr2, gr3, gr4, gr5;
	GridItemAdapter adapter;

	ArrayList<TimeSp> timeSps = new ArrayList<TimeSp>();

	Bussiness bussiness = new Bussiness();
	private BroadcastReceiver mReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_appointment);

		title = (TextView) findViewById(R.id.title);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);
		txtName = (TextView) findViewById(R.id.txt_name);

		bussiness = getIntent().getParcelableExtra("appoint");
		setData();

		grToday = (MyGridView) findViewById(R.id.gr_today);
		grTm = (MyGridView) findViewById(R.id.gr_tm);
		
		gr1 = (MyGridView) findViewById(R.id.gr_tm1);
		gr2 = (MyGridView) findViewById(R.id.gr_tm2);
		gr3 = (MyGridView) findViewById(R.id.gr_tm3);
		gr4 = (MyGridView) findViewById(R.id.gr_tm4);
		gr5 = (MyGridView) findViewById(R.id.gr_tm5);

		adapter = new GridItemAdapter(AllAppointmentActivity.this, timeSps);
		grToday.setAdapter(adapter);
		grTm.setAdapter(adapter);
		gr1.setAdapter(adapter);
		gr2.setAdapter(adapter);
		gr3.setAdapter(adapter);
		gr4.setAdapter(adapter);
		gr5.setAdapter(adapter);
		
		clickItem();
		
		init();

		receiverBroastCast();
	}
	
	void clickItem(){
		grToday.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				gotoStaff();
			}

		});

		grTm.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();

			}

		});
		
		gr1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();

			}

		});
		
		gr2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();

			}

		});
		
		gr3.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();

			}

		});
		
		gr4.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();

			}

		});
		
		gr5.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				gotoStaff();
			}

		});
	}

	void gotoStaff() {
		Intent t = new Intent(AllAppointmentActivity.this,ListStaffActivity.class);
		startActivity(t);
		overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
	}

	ArrayList<TimeSp> setData() {
		timeSps.add(new TimeSp("9:00AM"));
		timeSps.add(new TimeSp("10:00AM"));
		timeSps.add(new TimeSp("11:00AM"));
		timeSps.add(new TimeSp("12:00AM"));
		timeSps.add(new TimeSp("13:00AM"));
		timeSps.add(new TimeSp("14:00AM"));
		timeSps.add(new TimeSp("15:00AM"));
		timeSps.add(new TimeSp("16:00AM"));
		timeSps.add(new TimeSp("17:00AM"));
		timeSps.add(new TimeSp("18:00AM"));
		timeSps.add(new TimeSp("19:00AM"));
		timeSps.add(new TimeSp("20:00AM"));
		return timeSps;
	}

	void init() {
		title.setText("Appointment");
		txtName.setText(bussiness.getNameBussiness());
		btnBack.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_back:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				closeActivity();
			}
			break;

		default:
			break;
		}
		return false;
	}

	void receiverBroastCast() {
		IntentFilter intentFilter = new IntentFilter();

		intentFilter.addAction(Common.FINISH);

		mReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, final Intent intent) {
				String action = intent.getAction();
				if (action.equals(Common.FINISH)) {
					finish();
				}
			}
		};
		// register
		this.registerReceiver(mReceiver, intentFilter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(mReceiver);
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
	
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}
}
